﻿using Serilog;
using System.Reflection;
using ILogger = Serilog.Core.Logger;

namespace SmartAgricultureService.Core
{
    public sealed class Logger
    {
        private static Logger _instance;
        public static Logger Instance => _instance ??= new Logger();
        private readonly ILogger _logger;
        private Logger()
        {
            var assemblyLocation = Assembly.GetEntryAssembly()?.Location ?? Assembly.GetExecutingAssembly().Location;
            var folder = !string.IsNullOrEmpty(assemblyLocation)
                ? Path.GetDirectoryName(assemblyLocation) ?? AppDomain.CurrentDomain.BaseDirectory
                : AppDomain.CurrentDomain.BaseDirectory;
            var logFile = Path.Combine(folder, "sas_log", "log-.txt");
            _logger = new LoggerConfiguration().MinimumLevel.Debug() .WriteTo.Async(a => a.File(logFile,
                    rollOnFileSizeLimit: true,
                    fileSizeLimitBytes: 4194304,
                    rollingInterval: RollingInterval.Day)).CreateLogger();
        }
        public void LogError(string messageTemplate, Exception exception = null)
        {
            try
            {
                if (exception != null)
                    _logger.Error(exception, messageTemplate);
            }
            catch { }
        }
        public void LogInfo(string messageTemplate)
        {
            try
            {
                _logger.Information(messageTemplate);
            }
            catch { }
        }
        public void LogInfo(string messageTemplate, params object[] propertyValues)
        {
            try
            {
                _logger.Information(messageTemplate, propertyValues);
            }
            catch { }
        }
    }
}